﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.util.NumberUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class ProgressBarElementWriter extends TextFieldElementWriter {

	public ProgressBarElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		int width = ec.getWidth();
		if (width <= 0)
			width = 50;
		int height = ec.getHeight();
		if (height <= 0)
			height = 13;
		int percent = -1;
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (Checker.isDecimal(value)) {
			double d = Double.parseDouble(value);
			percent = NumberUtil.format0(d * 100D);
			value = Integer.toString(NumberUtil.format0(d * 100D));
			value = (new StringBuilder(String.valueOf(value))).append("%")
					.toString();
		}
		html.append("<TABLE cellpadding=\"0\" cellspacing=\"0\"><TR><TD>");
		html.append("<TABLE cellpadding=\"0\" cellspacing=\"0\" class=\"")
				.append("E68").append("\" width=\"").append(width).append(
						"px\" height=\"").append(height).append("px\"><TR>");
		if (percent > 100)
			html.append("<TD class=\"").append("E68O").append("\"></TD>");
		else if (percent >= 0) {
			html.append("<TD class=\"").append("E68N").append("\" width=\"")
					.append(percent).append("%\"></TD>");
			html.append("<TD width=\"").append(100 - percent).append(
					"%\"></TD>");
		} else {
			html.append("<TD class=\"").append("E68I").append("\"></TD>");
		}
		html.append("</TR></TABLE></TD>");
		html.append("<TD class=\"").append("E68T").append("\">").append(value)
				.append("</TD></TR></TABLE>");
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
		String value = ElementUtil.getValueOrDefaultWithFormat(ac, ec, row);
		html.append("<input type=\"hidden\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setValue(html, value);
		html.append("/>");
	}
}
